﻿using System;
using System.Collections.Generic;
using System.Reflection.Metadata;
using System.Text;
using SH;

namespace MTS
{

    public class mtObject : shISerializeable
    {
        protected uint m_nVer;
        protected string m_deviceID;
        protected string m_stationID;
        protected ulong m_timeStamp;

        public mtObject(uint nVer)
        {
            m_nVer = nVer;
        }

        public mtObject(mtObject other)
        {
            m_nVer = other.m_nVer;
            m_deviceID = other.m_deviceID;
            m_stationID = other.m_stationID;
            m_timeStamp = other.m_timeStamp;
        }

        //public virtual bool Equals(mtObject pObj)
        //{
        //    return false;
        //}

        public shISerializer serialize(shISerializer sc)
        {
            sc.serialize(ref m_nVer);
            sc.serialize(ref m_deviceID);
            sc.serialize(ref m_stationID);
            sc.serialize(ref m_timeStamp);

            return sc;
        }
        public uint Version { get => m_nVer; set => m_nVer = value; }
        public string DeviceID { get => m_deviceID; set => m_deviceID = value; }
        public ulong TimeStamp { get => m_timeStamp; set => m_timeStamp = value; }

        public string StationID { get => m_stationID; set => m_stationID = value; }
    };


    public class mtStationInfo : mtObject, shISerializeable
    {
        string m_StationName;   //站点名称
        string m_guid;
        string m_location;
        bool m_bConnect = false;
        emStationType m_stationType = emStationType.emSTT_Unknown;
        emProgramType m_programType = emProgramType.emPT_Unknown;
        List<string> m_macAddress = new List<string>();

        public        mtStationInfo()
            : base(0)

        {
        }

        public mtStationInfo(string stationName, string location, string guid, emStationType st, emProgramType pt, bool bConnect)
        : base(0)
        {
            m_bConnect = bConnect;
            m_stationType = st;
            m_programType = pt;
            m_StationName = stationName;
            m_location = location;
            m_guid = guid;
        }

        public new shISerializer serialize(shISerializer sc)
        {
            base.serialize(sc);
            sc.serialize(ref m_StationName);
            sc.serialize(ref m_guid);
            sc.serialize(ref m_location);
            sc.serialize(ref m_bConnect);
            sc.serialize(ref m_stationType);
            sc.serialize(ref m_programType);
            sc.serialize(ref m_macAddress);

            return sc;
        }

        public List<string> MacAddress { get => m_macAddress; set => m_macAddress = value; }

        public string StationName { get => m_StationName; set => m_StationName = value; }
        public string Location { get => m_location; set => m_location = value; }
        public bool IsConnected { get => m_bConnect; set => m_bConnect = value; }

        public string Guid { get => m_guid; set => m_guid = value; }
        public emStationType StationType { get => m_stationType; set => m_stationType = value; }
        public emProgramType ProgramType { get => m_programType; set => m_programType = value; }

    }

    public class mtRegisterInfo : mtObject, shISerializeable
    {
        emStationType m_stationType = emStationType.emSTT_Unknown;
        string m_StationName;   //站点名称
        emProgramType m_programType = emProgramType.emPT_Unknown;
        string m_programName;
        string m_location;
        string m_userName;
        string m_password;
        bool m_bAutoConnect = false;
        bool m_bAutoLogon = false;

        uint m_nMaxHeartFailCount;
        uint m_nConnectInterval;
        uint m_nHeartInterval;

        // add by tangyetao 2020/07/15 注册信息中添加当前站点的语言环境ID
        int m_nLanguageID = 0;

        public  mtRegisterInfo()
                : base(0)
        {
        }

        public mtRegisterInfo(emStationType stationType, string stationName, emProgramType programType, string programName,
                 string location, bool bAutoConnect = false, int nLanguageID = 0)
                : base(0)
        {
            m_bAutoConnect = bAutoConnect;
            m_bAutoLogon = false;
            m_StationName = stationName;
            m_location = location;
            m_nMaxHeartFailCount = 3;
            m_nConnectInterval = 5000;
            m_nHeartInterval = 15000;
            m_stationType = stationType;
            m_programType = programType;
            m_programName = programName;
            m_nLanguageID = nLanguageID;
        }

        public mtRegisterInfo(emStationType stationType, string stationName, emProgramType programType, string programName,
                 string location, bool bAutoConnect, string userName, string password, int nLanguageID = 0)
                : base(0)
        {

            m_bAutoConnect = bAutoConnect;
            m_bAutoLogon = false;
            m_StationName = stationName;
            m_location = location;
            m_nMaxHeartFailCount = 3;
            m_nConnectInterval = 5000;
            m_nHeartInterval = 15000;
            m_stationType = stationType;
            m_programType = programType;
            m_programName = programName;
            m_nLanguageID = nLanguageID;
            m_userName = userName;
            m_password = password;
            if (m_userName.Length != 0 && m_password.Length != 0)
                m_bAutoLogon = true;
        }


        public new shISerializer serialize(shISerializer sc)

        {
            base.serialize(sc);
            sc.serialize(ref m_StationName);
            sc.serialize(ref m_location);
            sc.serialize(ref m_bAutoConnect);
            sc.serialize(ref m_bAutoLogon);
            sc.serialize(ref m_userName);
            sc.serialize(ref m_password);
            sc.serialize(ref m_nMaxHeartFailCount);
            sc.serialize(ref m_nConnectInterval);
            sc.serialize(ref m_nHeartInterval);
            sc.serialize(ref m_stationType);
            sc.serialize(ref m_programType);
            sc.serialize(ref m_programName);
            sc.serialize(ref m_nLanguageID);
            return sc;
        }

        public string ProgramName { get => m_programName; set => m_programName = value; }


        public string StationName { get => m_StationName; set => m_StationName = value; }
        public string Location { get => m_location; set => m_location = value; }
        public bool IsAutoConnect { get => m_bAutoConnect; }
        public void SetAutoConnect(bool b) { m_bAutoConnect = b; }
        public bool IsAutoLogon { get => m_bAutoLogon; }
        public void SetAutoLogon(bool b) { m_bAutoLogon = b; }
        public string UserName { get => m_userName; set => m_userName = value; }
        public string Password { get => m_password; set => m_password = value; }

        public emStationType StationType { get => m_stationType; set => m_stationType = value; }
        public emProgramType ProgramType { get => m_programType; set => m_programType = value; }

        public uint MaxHeartFailCount { get => m_nMaxHeartFailCount; set => m_nMaxHeartFailCount = value; }
        public uint ConnectInterval { get => m_nConnectInterval; set => m_nConnectInterval = value; }
        public uint HeartInterval { get => m_nHeartInterval; set => m_nHeartInterval = value; }

        public int LanguageID { get => m_nLanguageID; set => m_nLanguageID = value; }

    }

    public class mtLogOnInfo : mtObject, shISerializeable
    {
        emStationType m_stationType = emStationType.emSTT_Unknown;
        string m_StationName;
        emProgramType m_programType = emProgramType.emPT_Unknown;
        string m_programName;
        string m_userName;
        string m_password;
        ulong m_status = 0xff;

        // add by tangyetao 2020/07/15 在注册信息中加入该站点当前的语言环境ID
        int m_nLanguageID;

        public   mtLogOnInfo()
                : base(0)

        {
        }

        public mtLogOnInfo(emStationType stationType, string stationName, emProgramType programType, string programName, int nLanguageID = 0)
        : base(0)
        {
            m_StationName = stationName;
            m_stationType = stationType;
            m_programType = programType;
            m_programName = programName;
            m_status = 0xff;
            m_nLanguageID = nLanguageID;
        }

        public mtLogOnInfo(emStationType stationType, string stationName, emProgramType programType, string programName,
         string userName, string password, int nLanguageID = 0)
        : base(0)

        {
            m_StationName = stationName;

            m_userName = userName;

            m_password = password;

            m_stationType = stationType;

            m_programType = programType;

            m_programName = programName;

            m_status = 0xff;

            m_nLanguageID = nLanguageID;
        }

        public mtLogOnInfo(mtRegisterInfo info)
        : base(0)

        {
            m_StationName = info.StationName;
            m_stationType = info.StationType;
            m_programType = info.ProgramType;
            m_programName = info.ProgramName;
            m_userName = info.UserName;
            m_password = info.Password;
            m_status = 0xff;

            m_nLanguageID = info.LanguageID;
        }


        public new shISerializer serialize(shISerializer sc)

        {
            base.serialize(sc);

            sc.serialize(ref m_StationName);
            sc.serialize(ref m_programName);
            sc.serialize(ref m_stationType);
            sc.serialize(ref m_programType);

            sc.serialize(ref m_userName);
            sc.serialize(ref m_password);

            sc.serialize(ref m_status);

            sc.serialize(ref m_nLanguageID);

            return sc;
        }

        public string ProgramName { get => m_programName; set => m_programName = value; }
        public string StationName { get => m_StationName; set => m_StationName = value; }

        public string UserName { get => m_userName; set => m_userName = value; }
        public string Password { get => m_password; set => m_password = value; }

        public emStationType StationType { get => m_stationType; set => m_stationType = value; }
        public emProgramType ProgramType { get => m_programType; set => m_programType = value; }

        public ulong Status { get => m_status; set => m_status = value; }


        public int LanguageID { get => m_nLanguageID; set => m_nLanguageID = value; }

    }

    public class mtHeartInfo : mtObject, shISerializeable
    {

        public
            mtHeartInfo()
                : base(0)

        {
        }
        public mtHeartInfo(ulong t)
        : base(0)

        {
            m_timeStamp = t;
        }

        public new shISerializer serialize(shISerializer sc)

        {
            base.serialize(sc);

            return sc;
        }

    };

    public class mtStationStatus : mtObject, shISerializeable
    {
        string m_stationName;
        string m_stationGuid;
        string m_location;
        ulong m_status = 0;
        public  mtStationStatus()
                : base(0)

        {
        }


        public new shISerializer serialize(shISerializer sc)

        {
            base.serialize(sc);
            sc.serialize(ref m_stationName);
            sc.serialize(ref m_stationGuid);
            sc.serialize(ref m_location);
            sc.serialize(ref m_status);
            return sc;
        }

        public string StationName { get => m_stationName; set => m_stationName = value; }
        public string StationGuid { get => m_stationGuid; set => m_stationGuid = value; }
        public string Location { get => m_location; set => m_location = value; }
        public ulong Status { get => m_status; set => m_status = value; }


    }

    public class mtCredential : mtObject, shISerializeable
    {
        string m_token;
        uint m_userID = 0;
        string m_userName;
        List<string> m_vecRoleName = new List<string>();
        HashSet<uint> m_setAccessID = new HashSet<uint>();

        public mtCredential()
                : base(0)

        {
        }

        public mtCredential(mtCredential other)
             : base(other)
        {
            m_token = other.m_token;
            m_userID = other.m_userID;
            m_userName = other.m_userName;
            m_vecRoleName.AddRange(other.m_vecRoleName);
            m_setAccessID.UnionWith(other.m_setAccessID);
        }

        public mtCredential(string token, uint userID, string userName, List<string> vecRoleName, HashSet<uint> setAccessID)
        : base(0)

        {
            m_token = token;
            m_userID = userID;
            m_userName = userName;
            m_vecRoleName = vecRoleName;
            m_setAccessID = setAccessID;
        }
        public new shISerializer serialize(shISerializer sc)

        {
            base.serialize(sc);
            sc.serialize(ref m_token);
            sc.serialize(ref m_userID);
            sc.serialize(ref m_userName);
            sc.serialize(ref m_vecRoleName);
            sc.serialize(ref m_setAccessID);
            return sc;
        }

        public void clear()
        {
            m_token = "";
            m_userID = 0;
            m_userName = "";
            m_vecRoleName.Clear();
            m_setAccessID.Clear();
        }

        public bool IsValid { get => m_userID != 0; }
        public string Token { get => m_token; }
        public uint UserID { get => m_userID; }
        public string UserName { get => m_userName; }
        public List<string> RolesNames { get => m_vecRoleName; }
        public HashSet<uint> AccessIDs { get => m_setAccessID; }

        public bool hasAccessID(uint accessID) => m_setAccessID.Contains(accessID);


    };

    public class mtInspectWorkMode : mtObject, shISerializeable
    {
        string m_token;
        string m_stationName;
        string m_stationGuid;
        emIPS3DWorkMode m_mode = emIPS3DWorkMode.emIPS3D_Unknown;

        public  mtInspectWorkMode()
                : base(0)
        {
        }

        public mtInspectWorkMode(emIPS3DWorkMode mode)
        : base(0)
        {
            m_mode = mode;
        }

        public mtInspectWorkMode(string token, emIPS3DWorkMode mode)
        : base(0)

        {
            m_token = token;
            m_mode = mode;
        }

        public mtInspectWorkMode(string stationName, string stationGuid, emIPS3DWorkMode mode)
        : base(0)
        {
            m_stationGuid = stationGuid;
            m_stationName = stationName;
            m_mode = mode;

        }

        public new shISerializer serialize(shISerializer sc)

        {
            base.serialize(sc);
            sc.serialize(ref m_token);
            sc.serialize(ref m_stationName);
            sc.serialize(ref m_stationGuid);
            sc.serialize(ref m_mode);

            return sc;
        }

        public string Token { get => m_token; set => m_token = value; }
        public emIPS3DWorkMode WorkMode { get => m_mode; set => m_mode = value; }
        public string StationName { get => m_stationName; set => m_stationName = value; }
        public string StationGuid { get => m_stationGuid; set => m_stationGuid = value; }


    };

    public class mtEventInfo : mtObject, shISerializeable
    {
        string m_source_id;//
        string m_location; //ip
        emStationType m_stationType = emStationType.emSTT_Unknown;
        string m_stationName;
        emProgramType m_programType = emProgramType.emPT_Unknown;
        string m_programName;
        int m_nUserId = 0;
        string m_userName;
        uint m_nRank = 0;
        uint m_nEventId = 0;
        uint m_nEventSubId = 0;
        string m_des;

        public  mtEventInfo()
                : base(0)
        {

        }

        public new shISerializer serialize(shISerializer sc)

        {
            base.serialize(sc);
            sc.serialize(ref m_source_id);
            sc.serialize(ref m_location);
            sc.serialize(ref m_userName);
            sc.serialize(ref m_stationName);
            sc.serialize(ref m_programName);
            sc.serialize(ref m_stationType);
            sc.serialize(ref m_programType);
            sc.serialize(ref m_userName);
            sc.serialize(ref m_nUserId);
            sc.serialize(ref m_nRank);
            sc.serialize(ref m_nEventId);
            sc.serialize(ref m_nEventSubId);
            sc.serialize(ref m_des);
            return sc;
        }
    };

    // 各站点上传给SMS的操作日志中的各个描述字段，如IPS3D中的：修改判图设置（判图模式：section1，判图超时时间：section2秒，超时判图结论：section3），section4.
    public class mtSectionInfo : mtObject, shISerializeable
    {
        // 该字符串是否需要翻译，如用户名s1就不需要翻译
        bool m_bNeedTranslation = false;
        // 描述字段的内容，当不需要翻译时，此处就填入不需要翻译的内容
        string m_strSectionDescribe;
        // 描述字段对应的资源ID（资源ID见文档），如果不需要翻译，就填0就行
        long m_nSectionID = 0x0000030402000000;
        // 描述内容的位置下标，从0开始
        int m_nIndex = 0;

        public bool NeedTranslation { get => m_bNeedTranslation; set => m_bNeedTranslation = value; }
        public string SectionDescribe { get => m_strSectionDescribe; set => m_strSectionDescribe = value; }
        public long SectionID { get => m_nSectionID; set => m_nSectionID = value; }
        public int Index { get => m_nIndex; set => m_nIndex = value; }

        public  mtSectionInfo()
                : base(0)
        {
        }

        public mtSectionInfo(bool bNeedTranslation, string strSectionDescribe, long nSectionID, int nIndex)
        : base(0)
        {
            m_nSectionID = nSectionID;
            m_bNeedTranslation = bNeedTranslation;
            m_strSectionDescribe = strSectionDescribe;
            m_nIndex = nIndex;
        }

        public new shISerializer serialize(shISerializer sc)

        {
            base.serialize(sc);
            sc.serialize(ref m_nSectionID);
            sc.serialize(ref m_bNeedTranslation);
            sc.serialize(ref m_strSectionDescribe);
            sc.serialize(ref m_nIndex);
            return sc;
        }
    }
    public class mtOperationInfo : mtObject, shISerializeable
    {
        // 当前日志的模板ID（具体见文档）
        long m_nLogTemplateID = 0x2041000000;
        // 当前日志的描述内容信息
        List<mtSectionInfo> m_vecSectionInfo = new List<mtSectionInfo>();
        // 操作的错误码，描述字段中如果没有错误码，那就直接填sh_ec_Success，也就是0
        shErrorCode m_nErrorCode = shErrorCode.sh_ec_Failed;
        public mtOperationInfo()
                : base(0)


        {
        }

        public mtOperationInfo(long nLogTemplateID, List<mtSectionInfo> vecSectionInfo, shErrorCode nErrorCode)
        : base(0)

        {
            m_nLogTemplateID = nLogTemplateID;
            m_vecSectionInfo = vecSectionInfo;
            m_nErrorCode = nErrorCode;
        }

        public List<mtSectionInfo> SectionInfo { get => m_vecSectionInfo; set => m_vecSectionInfo = value; }
        public long LogTemplateID { get => m_nLogTemplateID; set => m_nLogTemplateID = value; }

        public new shISerializer serialize(shISerializer sc)

        {
            base.serialize(sc);
            sc.serialize(ref m_nLogTemplateID);
            sc.serialize(m_vecSectionInfo);
            sc.serialize(ref m_nErrorCode);
            return sc;
        }
    }
    /// <summary>
    /// 通用接口的消息定义
    /// </summary>
    public class mtMsgRequest : mtObject, shISerializeable
    {
        string m_strToken;
        emMessageId m_msgId = emMessageId.emMsgId_Unknown;
        Blob m_msgData;

        public mtMsgRequest()
            : base(0)
        {
        }

        public mtMsgRequest(string strToken, emMessageId mEmMsgId, Blob msgData) : base(0)

        {
            m_strToken = strToken;
            m_msgId = mEmMsgId;
            m_msgData = msgData;
        }


        public string GetToken()
        {
            return m_strToken;
        }

        void SetToken(string strToken)
        {
            m_strToken = strToken;
        }

        emMessageId GetMsgId()
        {
            return m_msgId;
        }

        void SetMsgId(emMessageId msgId)
        {
            m_msgId = msgId;
        }

        Blob GetMsgData()
        {

            return m_msgData;
        }



        void SetMsgData(Blob msgData)
        {
            m_msgData = msgData;
        }




        public new shISerializer serialize(shISerializer sc)

        {
            sc.serialize(ref m_strToken);
            sc.serialize(ref m_msgId);
            sc.serialize(ref m_msgData);
            return sc;
        }


    };


}
